import type { Detailes, detailesType } from './../type/typings';

import type { ShopItemType, VideoItem, ShopcartItemType, AddressType, OrderItemType } from './../type/lidare';
import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import { getVideoList_l } from "@/api/lidare"
interface ShopParamsType {
  item: ShopItemType,
  type?: boolean
}

interface DetaileParamsType {
  item: detailesType,
  type?: boolean
}

export const useCounterStore = defineStore('counter', () => {
  //购物车
  const shopcartList = ref<Array<ShopcartItemType>>([]);
  shopcartList.value = JSON.parse(localStorage.getItem("shopcartList") as string) || [];
  const selectList = computed(() => {
    return shopcartList.value.filter(el => el.isSelect);
  });
  const allPrice = computed(() => {
    let num = 0;
    selectList.value.forEach(el => {
      num += el.count * (Number.parseFloat(el.salesPrice) / 100);
    })
    return num;
  })
  const changeShopCartList = ({ item, type = true }: ShopParamsType) => {
    if (type) {
      shopcartList.value = [item as ShopcartItemType, ...shopcartList.value];
    } else {
      shopcartList.value = [...shopcartList.value.filter(el => el.id != item.id)];
    }
    localStorage.setItem("shopcartList", JSON.stringify(shopcartList.value));
  };
  // 详情页购物车添加
  const changeDetailShopCartList = (item: ShopcartItemType) => {
    let shData = shopcartList.value.filter(el => el.id != item.id)
    shopcartList.value = [item as ShopcartItemType, ...shData];
    localStorage.setItem("shopcartList", JSON.stringify(shopcartList.value));
  };
  //修改购物车中的个别商品数量，是否选中状态
  const changeShopCartListItem = (item: ShopcartItemType) => {
    shopcartList.value = shopcartList.value.map(el => {
      if (el.id == item.id) {
        el.count = item.count;
        el.isSelect = item.isSelect;
      }
      return el;
    });
    localStorage.setItem("shopcartList", JSON.stringify(shopcartList.value));
  }
  //处理购物车的全体操作
  const changeShopCartAllItem = (list: Array<ShopcartItemType>) => {
    shopcartList.value = list;
    localStorage.setItem("shopcartList", JSON.stringify(shopcartList.value));
  }

  //支付后订单列表
  const orderList = ref<Array<OrderItemType>>([]);
  orderList.value = JSON.parse(localStorage.getItem("orderList") as string) || [];
  const changeOrderList = (list: Array<OrderItemType>) => {
    orderList.value = [...list, ...orderList.value];
    localStorage.setItem("orderList", JSON.stringify(orderList.value));
  }

  //视频数据
  const videoList = ref<Array<VideoItem>>([]);
  const changeVideoList = (list: Array<VideoItem>, mapList: Array<VideoItem> = []) => {
    videoList.value = [...mapList, ...list];
  };
  const getMoreVideoList = async () => {
    addVideoPage(1);
    let res = await getVideoList_l(videoPage.value);
    let list = res.data._data.lists;
    let temp: Array<VideoItem> = [];
    list.forEach((el: VideoItem) => {
      let check = true;
      try {
        temp.forEach(item => {
          if (item.id == el.id) check = false;
        });
      } catch { }
      if (check) {
        temp.push(el);
      }
    });
    list = temp;
    changeVideoList(list, videoList.value);
  }
  const videoPage = ref(-1);
  const addVideoPage = (value: number) => {
    if (value == 0) {
      videoPage.value = value;
      return;
    }
    videoPage.value += value;
  }
  // const count = ref(0)
  // const doubleCount = computed(() => count.value * 2)
  // function increment() {
  //   count.value++
  // }

  // 详情数据历史记录
  const phoneHistoryData = ref<Array<detailesType>>([]);
  phoneHistoryData.value = JSON.parse(localStorage.getItem('phoneHisList') as string) || [];
  const lookPhoneHisList = ((item: Detailes) => {
    let phData = phoneHistoryData.value.filter(el => el.id != item.id)
    phoneHistoryData.value = [item as detailesType, ...phData];
    localStorage.setItem("phoneHisList", JSON.stringify(phoneHistoryData.value));
  })

  // 详情数据收藏
  const collectData = ref<Array<detailesType>>([]);
  collectData.value = JSON.parse(localStorage.getItem('collectList') as string) || [];
  const collectPhoneList = ({ item, type = true }: DetaileParamsType) => {
    if (type) {
      collectData.value = [item as detailesType, ...collectData.value];
    } else {
      collectData.value = [...collectData.value.filter(el => el.id != item.id)]
    }
    localStorage.setItem("collectList", JSON.stringify(collectData.value));
  }

  // 详情页立即支付
  const payData = ref<OrderItemType>();
  const payTempPhoneData = (item: OrderItemType) => {
    payData.value = item
  }


  return { orderList, changeOrderList, allPrice, selectList, shopcartList, changeShopCartList, changeShopCartListItem, changeShopCartAllItem, videoList, changeVideoList, getMoreVideoList, videoPage, addVideoPage, collectPhoneList, collectData, lookPhoneHisList, phoneHistoryData, changeDetailShopCartList, payData, payTempPhoneData }
});

//地址
export const useAddressStore = defineStore('address', () => {
  interface AddressParams {
    item: AddressType,
    type?: number
  }
  const addressList = ref<Array<AddressType>>([]);
  addressList.value = JSON.parse(localStorage.getItem("addressList") as string) || [];
  const nowSelectAddress = ref<AddressType>();
  nowSelectAddress.value = JSON.parse(localStorage.getItem("nowSelectAddress") as string) || [];
  const changeSelectAddress = (item: AddressType) => {
    nowSelectAddress.value = item;
    localStorage.setItem("nowSelectAddress", JSON.stringify(nowSelectAddress.value));
  }
  const changeAddressList = ({ item, type = 0 }: AddressParams) => {
    if (type == 0) {
      if (item.isDefault) {
        addressList.value = [item, ...addressList.value.map(el => {
          el.isDefault = false;
          return el;
        })];
      } else {
        let temp = addressList.value.filter(el => el.isDefault);
        addressList.value = [...temp, item, ...addressList.value.filter(el => !el.isDefault)];
      }
    } else if (type == 1) {


    } else if (type == 2) {
      addressList.value = addressList.value.filter(el => el.id != item.id);
    }
    localStorage.setItem("addressList", JSON.stringify(addressList.value));
  }
  return { addressList, changeAddressList, nowSelectAddress, changeSelectAddress };
});

//用户状态
export const useUserInfo = defineStore("user", () => {
  const isLogin = ref(false);
  isLogin.value = localStorage.getItem("token") ? true : false;
  const changeIsLogin = (check: boolean) => {
    isLogin.value = check;
  }
  const user = reactive({});
  return { user, isLogin, changeIsLogin }
});
